﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace Room
{
    public class RoomRush : RoomBase
    {
        GameObject mainActor;

        bool isLoadComplete = false;

        public new Vector3 cameraAngle = new Vector3(0, 180f, 0);
        public new Vector3 cameraDelta = new Vector3(0, 1.5f, 4.5f);
        public Vector3 mainActorInitPosition = Vector3.zero;
        public const float mainActorRushDistance = 9f;
        public const float mainActorRushDistance0 = 7f;
        public bool isReadyForBattle = false;
        
        public string currentSceneName;
        public override void Init()
        {
            base.Init();
            isLoadComplete = false;
            mainActorRushTime = 0f;
            isReadyForBattle = false;
            currentSceneName = "";
            currentState = RoomRushState.None;
        }


        protected override void OnEnter(params object[] param)
        {
            if (!inited) {
                Init();
            }
            ManagerController.Instance.cameraTool.SwitchSceneCamera();
            StartCoroutine(LoadResource());
        }

        protected override void OnLeave()
        {
            UnloadResource();
        }

        protected IEnumerator LoadResource() {
            while (string.IsNullOrEmpty(StaticData.rushCurFloorSceneName))
            {
                yield return 0;
            }

            if (currentSceneName != StaticData.rushCurFloorSceneName)
            {
                yield return StartCoroutine(
                    AssetLoad.AssetBundleManager.Instance.LoadSceneAsync(SceneName.sceneFolder +
                   StaticData.rushCurFloorSceneName, StaticData.rushCurFloorSceneName));
                currentSceneName = StaticData.rushCurFloorSceneName;
            }

            var dh = Dict.Blo.DictHeroBlo.GetTableHero(StaticData.playerData.mainHeroId);
            while (true) {
                mainActor = RoomModelPool.Instance.GetModelFromCache(dh.resName);
                if (mainActor != null)
                {
                    break;
                }
                else {
                    yield return null;
                }
            }
            mainActor.transform.SetParent(transform);
            mainActor.transform.localPosition = mainActorInitPosition;
            mainActor.transform.localEulerAngles = new Vector3(0, 180, 0); 
            mainActor.transform.localScale = (float)dh.ScaleFactor * Vector3.one;
            mainActor.GetComponent<LegacyAnimation>().Play(AniName.STAND, WrapMode.Loop);
            CommonMethod.ChangeLayer(mainActor.transform, LayerName.SceneActor);
            OnLoadComplete();
        }

        protected void UnloadResource() {
            var dh = Dict.Blo.DictHeroBlo.GetTableHero(StaticData.playerData.mainHeroId);
            if (mainActor != null) {
                RoomModelPool.Instance.PutModelToCache(dh.resName, mainActor);
                mainActor = null;
            }
            isLoadComplete = false;
        }

        protected void OnLoadComplete() {
            sceneCamera.Set(cameraDelta, cameraAngle, cameraFOV);
            sceneCamera.SetTarget(mainActor.transform);
            StaticData.cameraRotate = Vector3.zero;
            isLoadComplete = true;
            Foreground();
            EasyTouchTool.Instance.Hide();
            LoadingPanel.Instance.HideDelay(0.5f);
            currentState = RoomRushState.Wait;
        }

        float mainActorRushSpeed = 5f;
        float mainActorRushTime = 0f;

        public enum RoomRushState
        {
            None,
            Wait,
            Rushing,
            RushingAndLoading,
            Done,
        }

        public RoomRushState currentState = RoomRushState.Wait;
        protected override void Tick()
        {
            if (currentState == RoomRushState.Rushing)
            {
                mainActor.transform.Translate(Vector3.forward * mainActorRushSpeed * Time.deltaTime);
                mainActorRushTime += Time.deltaTime;
                var dis = Vector3.Distance(mainActor.transform.localPosition, mainActorInitPosition);
                if (dis >= mainActorRushDistance0) {
                    LoadingPanel.Instance.ShowMask(0.5f);
                    currentState = RoomRushState.RushingAndLoading;
                }
            }
            else if (currentState == RoomRushState.RushingAndLoading)
            {
                mainActor.transform.Translate(Vector3.forward * mainActorRushSpeed * Time.deltaTime);
                mainActorRushTime += Time.deltaTime;
                var dis = Vector3.Distance(mainActor.transform.localPosition, mainActorInitPosition);
                if (dis >= mainActorRushDistance) {
                    OnMainActorRushComplete();
                    currentState = RoomRushState.Done;
                }
            }
            else
            {

            }
        }

        public void MainActorRush() {
            mainActorRushTime = 0f;
            mainActor.GetComponent<LegacyAnimation>().Play(AniName.RUN, WrapMode.Loop);
            currentState = RoomRushState.Rushing;
        }

        public void OnMainActorRushComplete() {
            mainActor.GetComponent<LegacyAnimation>().Play(AniName.STAND, WrapMode.Loop);
            StartCoroutine(ToBattle());
        }

        IEnumerator ToBattle() {
            while (!isReadyForBattle) {
                yield return null;
            }
            ManagerController.Instance.RushToBattle();
        }

        public bool IsRunning() {
            return currentState == RoomRushState.Rushing || currentState == RoomRushState.RushingAndLoading;
        }

        public bool IsLoadComplete()
        {
            return isLoadComplete;
        }

    }
}
